import Global from "@/Global";
import { getKey } from "@/util/conStr";
export function clearAll() {
	localStorage.clear();
}

//版本号
export function CheckVersion() {
	let version = localStorage.getItem("version");
	if (version) {
		return version == Global.version;
	} else return false;
}
export function AddVersion() {
	localStorage.setItem("version", Global.version);
}

//app版本判断 个人、企业
export function GetAPPUserType() {
	let type = localStorage.getItem("app_use_type");
	if (type) return type;
	else return "";
}
export function AddAPPUserType(type) {
	localStorage.setItem("app_use_type", type);
}

//获取个人搜索职位历史记录
export function getPersonSearchHistory() {
	let list = localStorage.getItem("person_search");
	if (list) return JSON.parse(list);
	else return new Array();
}

//加入个人搜索职位历史记录
export function addPersonSearchHistory(kw) {
	if (kw === "") return;
	let list = getPersonSearchHistory();
	if (list.indexOf(kw) < 0) {
		list.push(kw);
		localStorage.setItem("person_search", JSON.stringify(list));
	}
}

//清空个人搜索职位历史记录
export function clearPersonSearchHistory() {
	localStorage.removeItem("person_search");
}

//写入个人登录
export function addPersonLoginData(personId, token, mobileNum, resumeId) {
	let data = { personID: personId,token: token,mobileNum: mobileNum,resumeID: resumeId };
	localStorage.setItem("person_user", JSON.stringify(data));
	AddAPPUserType('P');
	AddVersion()
	setToken(token);
	if(resumeId){
		setResume();
	}
}

//写入个人自动登录
export function addPersonAutoLoginData(username = "", password = "") {
	let data = { username, password };
	localStorage.setItem("person_autologin", JSON.stringify(data));
}

//获取自动登录
export function getPersonAutoLoginData() {
	let dataStr = localStorage.getItem("person_autologin");
	return JSON.parse(dataStr);
}

//判断个人是否登录
export function checkPersonLogin() {
	let person = localStorage.getItem("person_user");
	if (person)
		return true;
	else
		return false;
}

//判断是否拥有简历
export function hasResume() {
	// let person = localStorage.getItem("person_user");
	// if (person && person.resumeID) { return true; }
	// return false;
	return localStorage.getItem('hasResume') == 'true';
}
//设置已有简历
export function setResume(){
	localStorage.setItem('hasResume',true);
}


//获取登录token
export function getToken() {
	return localStorage.getItem('token');
}
export function setToken(token){
	localStorage.setItem('token',token)
}
//获取用户编号
export function getPersonID() {
	let person = localStorage.getItem("person_user");
	if (person) { return JSON.parse(person).personID; }
	return "";
}
//获取绑定手机
export function getMobileNum() {
	let person = localStorage.getItem("person_user");
	if (person) { return JSON.parse(person).mobileNum; }
	return "";
}

//个人-地区-添加
export function addPersonArea(data) {
	localStorage.setItem("person_area", JSON.stringify(data));
}
//个人-地区-获取
export function getPersonArea() {
	let data = localStorage.getItem("person_area");
	if (!data) return null;
	return JSON.parse(data);
}

//个人-常用语
export function getPersonChangYongYu() {
	let data = localStorage.getItem("person_ChangYongYu");
	if (!data) {
		return [
			"你好，请问这个岗位现在还招人吗？",
			"你好，简历已投，请关注！",
			"你好，请问有五险一金吗？",
			"你好，请问什么时候可以面试？",
		];
	} else {
		return JSON.parse(data);
	}
}
//个人-常用语
export function addPersonChangYongYu(data) {
	localStorage.setItem("person_ChangYongYu", JSON.stringify(data));
}

//写入企业登录状态
export function addCompanyLoginData(companyID, companyIDEncrpt, token,memberType,isExpire) {
	let data = {companyID,companyIDEncrpt,token,memberType,isExpire};
	localStorage.setItem("company_user", JSON.stringify(data));
	AddAPPUserType('C');
	AddVersion();
	setToken(token);
}

//写入企业自动登录
export function addCompanyAutoLoginData(userName, passWord) {
	let data = {username:userName,password:passWord};
	localStorage.setItem("company_autologin", JSON.stringify(data));
}
export function getCompanyAutoLoginData() {
	let dataStr = localStorage.getItem("company_autologin");
	return JSON.parse(dataStr);
}

export function checkCompanyLogin() {
	let company_user = localStorage.getItem("company_user");
	if (company_user)
		return true;
	else
		return false;
}

export function getCompanyID() {
	let company = localStorage.getItem("company_user");
	if (company) { return JSON.parse(company).companyID; }
	return "";
}

//企业-常用语
export function getCompanyChangYongYu() {
	let data = localStorage.getItem("company_ChangYongYu");
	if (!data) {
		return [
			"你好，我对你的简历感兴趣，可以聊一下吗？",
			"你好，请问你还在找工作吗？",
			"你好，请投递下简历！",
		];
	} else {
		return JSON.parse(data);
	}
}
//企业-常用语
export function addCompanyChangYongYu(data) {
	localStorage.setItem("company_ChangYongYu", JSON.stringify(data));
}

export function switchUserType(usertype) {
	clearAll();
	AddVersion();
	AddAPPUserType(usertype);
}
